package org.example.ioc.pro5_ipc_import;


import org.example.ioc.pro5_ipc_import.other.Apple;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Random;

//用于扩展bean的功能
public class MyImportBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //1.spring容器中是否有Apple这个Bean
        boolean b = registry.containsBeanDefinition(Apple.class.getName());
        Random r = new Random();
        String beanId = Apple.class.getSimpleName().substring(0,1).toLowerCase()+Apple.class.getSimpleName().substring(1)+r.nextInt(10000);
        if(b==false){
            //2.如果没有，就注册一个Bean
            RootBeanDefinition rb = new RootBeanDefinition(Apple.class);
            registry.registerBeanDefinition(beanId,rb);
        }
    }
    /**
     * AnnotationMetadata：当前类的注解信息
     * BeanDefinitionRegistry：BeanDefinition注册类
     * BeanNameGenerator：Bean名称生成器
     * */



}
